package reader;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.shhic.itsed.mws.deviceintf.AntennaProtocol;
import com.shhic.itsed.mws.deviceintf.RawTagBank;
import com.shhic.itsed.mws.deviceintf.RawTagField;
import com.shhic.itsed.mws.deviceintf.ReaderCommand;
import com.shhic.itsed.mws.deviceintf.ReaderCommandType;
import com.shhic.itsed.mws.deviceintf.ReaderLinkStatusEnum;
import com.tjkcht.midware.reader.Rd30Manager;

@RunWith(Parameterized.class)
public class ReadManagerSingle {
	
	public ReadManagerSingle(RawTagBank bank,int offset,int length){
		this.bank = bank;
		this.offset = offset;
		this.length = length;
		
	}
	
	private RawTagBank bank;
	private int offset;
	private int length;
	private Rd30Manager rd;
	private String readerIp;
	private int port;
	public ResultCallBack callBack = new ResultCallBack();

    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(
                new Object[][]{
                        {RawTagBank.EPCBANK,0, 1},
                        {RawTagBank.EPCBANK,0, 10},
                        {RawTagBank.EPCBANK,0, 33},
                        {RawTagBank.EPCBANK,2, 30},
                        {RawTagBank.EPCBANK,4, 10},
                     
                        {RawTagBank.RESERVED,0, 2},
                        {RawTagBank.RESERVED,1, 10},
                        {RawTagBank.RESERVED,2, 4},
                        {RawTagBank.RESERVED,10, 1},
                      
                        
                        {RawTagBank.TID,0, 4},
                        {RawTagBank.TID,0, 10},
                        {RawTagBank.TID,0, 20},
                    
                        {RawTagBank.USER,0, 4}, 
                        {RawTagBank.USER,0, 10}
                       // {RawTagBank.USER,0, 20}
               
                 

                }

        );
    }

	
	@Before
	public void  setUp() throws Exception {
		 rd = new Rd30Manager();
		 readerIp = "192.168.11.171";
		 port = 5084;	
		 System.out.println("in setUp!");
	}
	
	@After
	public void tearDown() throws Exception {
		
		rd.close();
		System.out.println("in tearDown!");
	}

	
	@Test
	public void testCmd() throws InterruptedException{
		rd.initReader(readerIp, port);
		System.out.println("after initReader");
		
		long current = System.currentTimeMillis();
		long now =  System.currentTimeMillis();
		while(rd.queryDeviceLinkstatus(readerIp)!=ReaderLinkStatusEnum.CONNECTED && now-current < 30000){
			System.out.println(rd.queryDeviceLinkstatus(readerIp));
			Thread.sleep(1000);
			now =  System.currentTimeMillis();
		
		}
		ReaderCommand cmd = new ReaderCommand();
		cmd.setReaderIp(readerIp);
		cmd.setReaderPort(port);
		cmd.setReaderCommandType(ReaderCommandType.SINGLE);
		AntennaProtocol ant = new AntennaProtocol();
		ant.setAntennaNo(1);
		Set<String> protocols = new HashSet<String>();
		protocols.add(ant.P_EPC);
		
		ant.setProtocols(protocols);
		
		Set<AntennaProtocol> antennaProtocolSet = new HashSet<AntennaProtocol>();
		
		antennaProtocolSet.add(ant);
		cmd.setAntennaProtocolSet(antennaProtocolSet);
		
		List<RawTagField> readTagFieldList = new ArrayList<RawTagField>();
		RawTagField tagField = new RawTagField();
		tagField.setBank(bank);
		tagField.setLength(length);
		tagField.setOffset(offset);
		readTagFieldList.add(tagField);
	
		cmd.setReadTagFieldList(readTagFieldList);
		cmd.setReaderCommandType(ReaderCommandType.SINGLE);
		
		rd.dispatchReadCommand(readerIp, cmd, 1000, callBack);
		Thread.sleep(10000);
		rd.stopReaderCommand(readerIp);
			
		
		
	}

}
